<?php
App::uses('AppController', 'Controller');
App::uses('AppUtility','Lib');
/**
 * Transactions Controller
 *
 */
class MoneyManagerController extends AppController {
	public $uses = array('User','Transaction');
	public function beforeFilter() {
			parent::beforeFilter();
			$this->layout = 'admin';
	}
	/**
	 * Function Index
	 * @author MinhPhan
	 */
	public function index(){
		$allUser = $this->User->find('all');
		$this->set('allUser',$allUser);
		if ($this->request->query) {
			if(isset($this->request->query['type'])){
				$arrData = $this->GetUserByType($this->request->query['type']);
			}
			if (isset($this->request->query['u'])){
				$uid = base64_decode($this->request->query['u']);
				if($uid !='')
					$arrData = $this->GetUserByName($uid);
				else
					$arrData = $this->User->find('all');
			}
		}else{
			$this->paginate = array(
					'fields'        => '*',
					'limit'         => 4
			);
			$arrData = $this->paginate('User');
			
		}
		$dataView = $this->zendHTML($arrData);
		$this->set('Dataview',$dataView);
		
	}
	/**
	 * GetUserByType
	 * @author Minh Phan
	 */
	public function GetUserByType($type){
		$arrConditons= array(
					'type_advice' => $type
				);
		
		try {
			$this->paginate = array(
					'conditions'	=> $arrConditons,
					'fields'        => '*',
					'limit'         => 4
			);
			$arrData = $this->paginate('User');
			
			return $arrData;
		} catch (Exception $ex) {
			$this->log($ex->getMessage(),LOG_ERROR);
		}
	}
	/**
	 * GetUserByName
	 * @author Minh Phan
	 * 
	 */
	public function GetUserByName($uid){
		$arrConditions = array(
						'id'=>$uid
				);
		try {
			$this->paginate = array(
					'conditions'	=> $arrConditions,
					'fields'        => '*',
					'limit'         => 4
			);
			$arrData = $this->paginate('User');
			return $arrData;
		} catch (Exception $ex) {
			$this->log($ex->getMessage(),LOG_ERROR);
		}
	}
	
	/**
	 * Function ZendHTML
	 * @author Minh Phan
	 * @param unknown $arrUser
	 * @return string
	 */
	public function zendHTML($arrData){
		if($arrData){
			foreach ($arrData as $item){
				//$totalTime = $item['Transaction']['start_time'] - $item['Transaction']['end_time'];
				$dataView[]=	"<tbody>
										<tr class='odd'>
											<td>". $item['User']['id']."</td>
											<td class = 'username'>". $item['User']['username']."</td>
											<td>". $item['User']['skype']."</td>
											<td>".$item['User']['email']."</td>
											<td>".$item['User']['money_in']."</td>
											<td>".$item['User']['total_transaction']."</td>
											<td>".$item['User']['total_time_advice']."</td>
											<td>".$item['User']['total_money']."</td>
										</tr>
	
									</tbody>
									";
			}
			return $dataView;
		}
	}

}
